const express = require("express")
const path = require("path")
const bodyParser = require("body-parser")
const studentRoutes = require("./routes/student")

const app = express()
const PORT = process.env.PORT || 3000


app.set("view engine", "ejs")
app.set("views", path.join(__dirname, "views"))


app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
app.use(express.static(path.join(__dirname, "public")))


app.use("/student", studentRoutes)


app.get("/", (req, res) => {
  res.redirect("/student/list")
})


app.use((req, res) => {
  res.status(404).send("页面未找到")
})


app.use((err, req, res, next) => {
  console.error(err.stack)
  res.status(500).send("服务器内部错误")
})

app.listen(PORT, () => {
  console.log(`服务器运行在 http://localhost:${PORT}`)
  console.log(`学生管理系统访问地址: http://localhost:${PORT}/student/list`)
})

module.exports = app
